//
//  CaseManageController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/18.
//  Copyright © 2016年 Demos. All rights reserved.
//
//0  idCard    phone  illness_rb_img_sel.png
//name

#import "CaseManageController.h"
#import "CaseManageCell.h"
#import "AddCaseController.h"
#import "CaseDTO.h"

@interface CaseManageController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong) UIImageView *iconV;

//四个小图标
@property (nonatomic, strong) UIImageView *img1;
@property (nonatomic, strong) UIImageView *img2;
@property (nonatomic, strong) UIImageView *img3;
@property (nonatomic, strong) UIImageView *img4;

@property (nonatomic, strong) UILabel *nameLab;

@property (nonatomic, strong) UILabel *sexLab;

@property (nonatomic, strong) UILabel *ageLab;

@property (nonatomic, strong) UILabel *IDLab;

@property (nonatomic, strong) UILabel *phoneLab;

@property (nonatomic, strong) UILabel *provienceLab;

///  tableview
@property (nonatomic, strong) UITableView *tableview;

///  修改个人信息
@property (nonatomic, strong) UIButton *updateInfoButton;

///  添加病例
@property (nonatomic, strong) UIButton *addCaseButton;


@property (nonatomic, strong) NSMutableArray *DTOArray;


@end

@implementation CaseManageController

static NSString  *ID = @"CityID";


- (void)viewDidLoad {
    [super viewDidLoad];

//    AddCaseController *addCaseVC = [[AddCaseController alloc] init];
//    
//    [self.navigationController pushViewController:addCaseVC animated:YES];
    
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    [self loadDtoData];

}

- (void)loadDtoData{
    
    //路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [path stringByAppendingPathComponent:@"case.plist"];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    
//    loggerImpt(@"拿到数组的个数=%ld",arr.count);
    
    [self.DTOArray removeAllObjects];
    
    for (int i = 0; i < arr.count; ++i) {
        
         CaseDTO *dto = [NSKeyedUnarchiver unarchiveObjectWithData:arr[i]];
        
        [self.DTOArray addObject:dto];
    }
//    打印的数组里面是一个二进制模型
//    loggerImpt(@"arr = %@",arr);
    
    [self.tableview reloadData];
}


- (void)setupUI{

    self.title = @"病例管理";
    
    [self addSubViews];
    [self addLayouts];
}

- (void)addSubViews{
    
    //1.
    self.iconV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"good_image_default"]];
    [self.view addSubview:self.iconV];
    [self.view addSubview:self.iconV];
    
    //四个小图标
    self.img1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0"]];
    [self.view addSubview:self.img1];
    self.img2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"idCard"]];
    [self.view addSubview:self.img2];
    self.img3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone"]];
    [self.view addSubview:self.img3];
    self.img4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
    [self.view addSubview:self.img4];
    
    //2.
    self.nameLab = [[UILabel alloc] init];
    [self.view addSubview:self.nameLab];
    self.nameLab.text = @"王二狗";
    [self.view addSubview:self.nameLab];
    self.nameLab.font = FONT(19);

    
    //3.
    self.sexLab = [[UILabel alloc] init];
    [self.view addSubview:self.sexLab];
    self.sexLab.text = @"男";
    [self.view addSubview:self.sexLab];
    self.sexLab.font = FONT(18);
    
    //4.
    self.ageLab = [[UILabel alloc] init];
    [self.view addSubview:self.ageLab];
    self.ageLab.text = @"28";
    [self.view addSubview:self.ageLab];
    self.ageLab.font = FONT(18);

    //5.
    self.IDLab = [[UILabel alloc] init];
    [self.view addSubview:self.IDLab];
    self.IDLab.text = @"2342342342342";
    [self.view addSubview:self.IDLab];
    self.IDLab.font = FONT(18);

    //6.
    self.phoneLab = [[UILabel alloc] init];
    [self.view addSubview:self.phoneLab];
    self.phoneLab.text = @"13157132432";
    [self.view addSubview:self.phoneLab];
    self.phoneLab.font = FONT(18);

    //7.
    self.provienceLab = [[UILabel alloc] init];
    [self.view addSubview:self.provienceLab];
    self.provienceLab.text = @"上海市";
    [self.view addSubview:self.provienceLab];
    self.provienceLab.font = FONT(18);

    //8.
    [self.view addSubview:self.updateInfoButton];
    
    //9.
    [self.view addSubview:self.tableview];
    
    //10.
    [self.view addSubview:self.addCaseButton];

}

- (void)addLayouts{

    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(64+20);
        make.size.mas_equalTo(CGSizeMake(90, 90));
    }];
    
    [self.sexLab mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.mas_equalTo(self.iconV);
        make.left.mas_equalTo(self.iconV.mas_right).offset(45);
        make.height.mas_equalTo(25);
    }];
    
    [self.ageLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.iconV);
        make.left.mas_equalTo(self.sexLab.mas_right).offset(30);
        make.height.mas_equalTo(25);
    }];
    
    [self.IDLab mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.mas_equalTo(self.sexLab.mas_bottom).offset(5);
        make.left.mas_equalTo(self.iconV.mas_right).offset(45);
        make.height.mas_equalTo(25);
    }];
    
    [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
     
        make.top.mas_equalTo(self.IDLab.mas_bottom).offset(5);
        make.left.mas_equalTo(self.iconV.mas_right).offset(45);
        make.height.mas_equalTo(25);
    }];
    
    
    [self.provienceLab mas_makeConstraints:^(MASConstraintMaker *make) {
     
        make.top.mas_equalTo(self.phoneLab.mas_bottom).offset(5);
        make.left.mas_equalTo(self.iconV.mas_right).offset(45);
        make.height.mas_equalTo(25);
    }];
    
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.iconV.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.iconV.mas_centerX);
        make.height.mas_equalTo(30);
    }];
    
    
    [self.img1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 18));
        make.left.mas_equalTo(self.iconV.mas_right).offset(15);
        make.centerY.equalTo(self.sexLab);
    }];
    [self.img2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 17));
        make.left.mas_equalTo(self.iconV.mas_right).offset(15);
        make.centerY.equalTo(self.IDLab);
    }];
    [self.img3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 20));
        make.left.mas_equalTo(self.iconV.mas_right).offset(15);
        make.centerY.equalTo(self.phoneLab);
    }];
    [self.img4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 17));
        make.left.mas_equalTo(self.iconV.mas_right).offset(15);
        make.centerY.equalTo(self.provienceLab);
    }];
    
    //修改个人信息
    [self.updateInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(15);
    }];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.updateInfoButton.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.addCaseButton.mas_top).offset(-10);
    }];
    
    
    [self.addCaseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(self.view).offset(-15);
    }];

}

#pragma mark  - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//    loggerInfo(@"刷新的最终个数：%ld",self.DTOArray.count);
    return self.DTOArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    CaseManageCell *cell = (CaseManageCell *)[tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

//    CaseManageCell *cell = (CaseManageCell *)[tableView dequeueReusableCellWithIdentifier:ID];
    
//    if (!cell) {
//        
//        cell = [[CaseManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    CaseDTO *dto = self.DTOArray[indexPath.row];
    
    cell.dto = dto;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [SVPAlertManager alertInfoStatus:@"正在开发中"];
}


#pragma mark  - Actions
///点击病例
- (void)clickBtn:(UIButton *)btn{

    AddCaseController *addCaseVC = [[AddCaseController alloc] init];
    
    [self.navigationController pushViewController:addCaseVC animated:YES];
    
     loggerWarn(@"添加病例");
}

#pragma mark  - Lazy
-(UITableView *)tableview{
    
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [UIView new];
        _tableview.rowHeight = 120;
        
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableview registerClass:[CaseManageCell class] forCellReuseIdentifier:ID];
        
    }
    return _tableview;
}

-(UIButton *)updateInfoButton{
    
    if (_updateInfoButton == nil) {
        _updateInfoButton = [[UIButton alloc] init];
        _updateInfoButton.backgroundColor = LIGHT_GRAY_COLOR;
    }
    return _updateInfoButton;
}

-(UIButton *)addCaseButton{
    
    if (_addCaseButton == nil) {
        _addCaseButton = [[UIButton alloc] init];
        [_addCaseButton setTitle:@"添加病例" forState:UIControlStateNormal];
        [_addCaseButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        _addCaseButton.layer.cornerRadius = 2;
        _addCaseButton.layer.masksToBounds = YES;
        _addCaseButton.backgroundColor = kThemeColor;

    }
    return _addCaseButton;
}


-(NSMutableArray *)DTOArray{
    
    if (_DTOArray == nil) {
        _DTOArray = [NSMutableArray arrayWithCapacity:4];
    }
    return _DTOArray;
}

@end
